const service = require('../../../utils/utilService.js');
Page({
  data: {
    dataSource: [],
    totalCount: 0,
    type:1,
    hasNextPage:false,
    isLoading: true,
  },
  onLoad: function (options) {
    this.setData({
      type: options.type
    })

    this.page = 0;
    //判断 -5是好友发布的商品
    if (options.type == -5) {
      wx.setNavigationBarTitle({
        title: "友物"
      })
      this.requestThem(false,function(){

      })
    } else {
      this.request(false, function () {

      })
    }
  },
  requestThem: function (addMore,callBack) {
    var that = this
    service.friendsGoods(this.page,function (data) {
      typeof callBack == 'function' && callBack()
      if (addMore){
        var newData = that.data.dataSource.concat(data.items)
        that.setData({ dataSource: newData, totalCount: data.totalCount, hasNextPage: data.hasNextPage });
      }else{
        that.setData({ dataSource: data.items, totalCount: data.totalCount, hasNextPage: data.hasNextPage, isLoading:false  });
      }
    });
  },
  request: function (addMore,callBack) {
    var that = this
    service.goodsCanBy(this.page,function (data) {
      wx.hideLoading()
      if (addMore) {
        var newData = that.data.dataSource.concat(data.items)
        that.setData({ dataSource: newData, totalCount: data.totalCount, hasNextPage: data.hasNextPage });
      } else {
        that.setData({ dataSource: data.items, totalCount: data.totalCount, hasNextPage: data.hasNextPage });
      }
      typeof callBack == 'function' && callBack()

    });
  },

  onReachBottom: function () {
    this.page += 1
    if (this.data.hasNextPage) {
      //判断 -5是好友发布的商品
      if (this.data.type == -5) {
        this.requestThem(true, function () {

        })
      } else {
        this.request(true, function () {

        })
      }
    }
  },

  onPullDownRefresh() {
    this.page = 0
    //判断 -5是好友发布的商品
    if (this.data.type == -5) {
      this.requestThem(false, function () {
        wx.stopPullDownRefresh();
      })
    } else {
      this.request(false, function () {
        wx.stopPullDownRefresh();
      })
    }
  }
});